<?php
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$newsTable = 'hellozjx_mapwxapp_news';
$teacherTable = 'hellozjx_mapwxapp_teacher';

if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $keyword = trim($_GPC['keyword']);
    $condition = '';
    if (!empty($keyword)) {
        $condition = " AND title LIKE '%{$keyword}%'";
    }
    $list = pdo_fetchall("SELECT id,sort,title,status,create_time FROM " . tablename($newsTable) . " WHERE status > 0 " . $condition . " ORDER BY sort DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($newsTable) . " WHERE status > 0 " . $condition);
    $pager = pagination($total, $pindex, $psize);
} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    $title = $_GPC['title'];
    if (checksubmit('submit')) {
        if(empty($_GPC['content']) || empty($_GPC['title']) || empty($_GPC['status']) || empty($_GPC['teacherId']))
            message('标题、内容、发布人、发布状态不可为空！', $this->createWebUrl('news', array('op' => 'display')), 'error');
        $where = '';
        if(!empty($id))
            $where = ' AND id<>'.$id;
        $isExists = pdo_fetch("select id from ".tablename($newsTable) ." WHERE title = '{$title} and status<>0' ".$where);
        if($isExists)
            message('操作失败，已存在同名新闻！', $this->createWebUrl('news', array('op' => 'display')), 'error');
        //查看新闻标题是否重复
        $data = array(
            'uniacid' => $_W['uniacid'],
            'sort' => $_GPC['sort'],
            'title' => $title,
            'content' => htmlspecialchars_decode($_GPC['content']),
            'status' => trim($_GPC['status']),
            'teacher_id' => trim($_GPC['teacherId']),
            'create_time' => date('Y-m-d H:i:s',time()),
            'update_time' => date('Y-m-d H:i:s',time())
        );
        if (!empty($id)) {
            unset($data['createtime']);
            pdo_update($newsTable, $data, array('id' => $id));
        } else {
            pdo_insert($newsTable, $data);
            $id = pdo_insertid();
        }
        message('操作成功！', $this->createWebUrl('news', array('op' => 'display')), 'success');
    }
    $news = pdo_fetch("select * from " . tablename($newsTable) . " where id=:id and uniacid=:uniacid limit 1", array(":id" => $id, ":uniacid" => $_W['uniacid']));
    $teacherList = pdo_fetchall("select name,id from ".tablename($teacherTable)." where status=1");
}elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $news = pdo_fetch("SELECT id FROM " . tablename($newsTable) . " WHERE id = '{$id}' AND uniacid=" . $_W['uniacid'] . "");
    if (empty($news)) {
        message('抱歉，文章不存在或是已经被删除！', $this->createWebUrl('news', array('op' => 'display')), 'error');
    }
    pdo_update($newsTable, ['status'=>0], array('id' => $id));
    message('删除成功', referer(), 'success');
}
include $this->template('news');